// 实现这个项目的构建任务
/**
 * 1、无法将node_modules 中的文件打包出来，useref 提取不出来
 */
const sass = require('node-sass');
const bs = require('browser-sync')
const loadGruntTasks = require('load-grunt-tasks')
module.exports = grunt =>{
    // grunt 配置
    grunt.initConfig({
        sass:{
            options: {
                implementation: sass,
                sourceMap: true
            },
            main: {
                files: {
                    'dist/assets/styles/main.css': 'src/assets/styles/main.scss'
                }
            }
        },

        // swig 编译
        web_swig: {
            options: {
              swigOptions:{
                cache: false
              },
              getData: function(tpl){
                  return  {
                    menus: [
                      {
                        name: 'Home',
                        icon: 'aperture',
                        link: 'index.html'
                      },
                      {
                        name: 'Features',
                        link: 'features.html'
                      },
                      {
                        name: 'About',
                        link: 'about.html'
                      },
                      {
                        name: 'Contact',
                        link: '#',
                        children: [
                          {
                            name: 'Twitter',
                            link: 'https://twitter.com/w_zce'
                          },
                          {
                            name: 'About',
                            link: 'https://weibo.com/zceme'
                          },
                          {
                            name: 'divider'
                          },
                          {
                            name: 'About',
                            link: 'https://github.com/zce'
                          }
                        ]
                      }
                    ],
                    pkg: require('./package.json'),
                    date: new Date()
                  }
              }
            },
            main: {
                expand: true,
                cwd: 'src/',//源文件文件夹
                src: "**/*.html",//后缀名匹配
                dest: "dist/"//目标文件夹
            },
          },
        // 清理文件的插件配置
        clean: {
            temp: 'dist/**'
        },
        // babel 编译
        babel: {
            main: {
                options: {
                    presets: ['@babel/preset-env']
                },
                files: {
                    'dist/assets/scripts/main.js': 'src/assets/scripts/main.js'
                }
            }
        },

        // 图片编译
        imagemin: {
            dist: {
                options: {
                    optimizationLevel: 3 //定义 PNG 图片优化水平
                },
                files: [{
                    expand: true,
                    cwd: 'src/assets/images/',   // 图片在imagemin目录下
                    src: ['**/*'], // 优化 imagemin 目录下所有 png/jpg/jpeg 图片
                    dest: 'dist/assets/images/' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                },
                {
                    expand: true,
                    cwd: 'src/assets/fonts/',   // 图片在imagemin目录下
                    src: ['**/*'], // 优化 imagemin 目录下所有 png/jpg/jpeg 图片
                    dest: 'dist/assets/fonts/' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
                }]
            }
        },

        //监视功能
        watch: {
            js: {
                //文件地址
                files: ['src/assets/scripts/*.js'],
                //执行的任务
                tasks: ['babel','reload']
            },
            css: {
                //文件地址
                files: ['src/assets/styles/*.scss'],
                //执行的任务
                tasks: ['sass','reload']
            },
            html: {
                //文件地址
                files: ['src/*.html'],
                //执行的任务
                tasks: ['web_swig', 'reload']
            }
        },
        // 处理文件中带 / 开头的路径
        useref: {
            html: 'dist/*.html',
            temp: '.'
        }
        
    })
    // 启动服务
    grunt.registerTask('serve',function(){
        const done = this.async()
        bs.init({
            open:false,
            port: 8080,
            server: {
                baseDir: ["dist", "public", "src",'.'],
                files: 'dist/**'
            }
        })
        // 启动服务后开启服务的重启任务
        grunt.registerTask('reload',()=>{
            bs.reload()
        })
    })

    // 打包使用
    grunt.registerTask('build',['clean','sass','babel','web_swig','imagemin','useref'])
    // 开发环境
    grunt.registerTask('dev',['clean','sass','babel','web_swig','serve'])

    // 自动加载配置
    loadGruntTasks(grunt)
}